classdef GaltonStrategyGMV < TradingStrategy
    %This is an class for the Naive Markowitz trading strategies
    properties
       name =  {'Galton GMV'};
       galtonCorrection;
     end
    
    methods
        function obj = strategyCalculate(obj, t, r_past, r_plus1, all_returns )
                
                post_window = obj.galtonCorrection.post_window;
                galton_coeffs = obj.galtonCorrection.galton_coeffs;
                %pre_window = obj.galtonCorrection.pre_window;
                
                basisMeans=mean(r_past)';
                cov_H=cov(r_past);
                corr_H=corr(r_past);
                var_H=diag(cov_H);    

                corr_Gal=nanmean(galton_coeffs(1:t-post_window,1,2,1))+nanmean(galton_coeffs(1:t-post_window,2,2,1))*corr_H;
                corr_Gal(eye(size(corr_Gal))==1)=1;

                var_Gal=var_H*nanmean(galton_coeffs(1:t-post_window,2,3,1))+nanmean(galton_coeffs(1:t-post_window,1,3,1));
                var_Gal=max(var_Gal,0);

                cov_Gal=diag(var_Gal)^.5*corr_Gal*diag(var_Gal)^0.5;% the corrected covariance matrix

                k=min(eig(cov_Gal));

                if k<=0
                     cov_Gal = cov_Gal + eye(size(cov_Gal))*(-2*k);
                end
                
                
                miu_MRC=basisMeans*nanmean(galton_coeffs(1:t-post_window,2,4,1))+nanmean(galton_coeffs(1:t-post_window,1,4,1));
                
%                 basisCov = cov_Gal;
%                 basisMeans = miu_MRC;

                %----------------------------------------------------------------------
                % Get the returns and variance of the MRC strategy

%                 w_GMV_MRC=cov_Gal\ones(size(miu_MRC));
%                 w_GMV_MRC=w_GMV_MRC/abs(sum(w_GMV_MRC));

 
              %weights = w_GMV_MRC;

            
%             obj.strategyExpectedVariance(t)=weights'*basisCov*weights;
% 
%             obj.strategyExpectedReturns(t)=weights'*basisMeans;
%             obj.strategyReturns(t)=weights'*r_plus1;
%             
%             obj = obj.strategyTurnover(t,all_returns,weights);
            
            weightsGMV = obj.setMVStrategyParams(cov_Gal,ones(size(basisMeans)));              
            obj = obj.executeCommonElements(t,weightsGMV,cov_Gal,miu_MRC, r_plus1,all_returns);    
        end
        
  

    end
    
end

